package com.example.sdtreader;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class SuccessActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_success);

        // 获取传递的参数
        String cameraPhotoPath = getIntent().getStringExtra("cameraPhotoPath");
        String idCardInfo = getIntent().getStringExtra("idCardInfo");

        // 显示照片
        if (cameraPhotoPath != null) {
            ImageView imageView = findViewById(R.id.iv_image1);
            Bitmap bitmap = getLoacalBitmap(cameraPhotoPath);
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            }
        }

        // 解析并显示身份证信息
        if (idCardInfo != null) {
            String[] info = idCardInfo.split("\\|");
            if (info.length >= 6) { // 确保数组有足够的元素
                // 设置姓名
                TextView nameTextView = findViewById(R.id.tv_name);
                nameTextView.setText("姓名：" + info[0]);

                // 设置身份证号
                TextView idCardTextView = findViewById(R.id.tv_id_card);
                idCardTextView.setText("身份证号：" + info[5]);

                // 设置班级信息（这里使用默认值，因为身份证信息中没有这些字段）
                TextView classGradeTextView = findViewById(R.id.tv_class_grade);
                classGradeTextView.setText("班级：14班");

                TextView classNameTextView = findViewById(R.id.tv_class_name);
                classNameTextView.setText("班名：智慧班");

                TextView teacherTextView = findViewById(R.id.tv_teacher);
                teacherTextView.setText("班主任：刘金伟");

                // 设置宿舍信息（这里使用默认值）
                TextView roomNumberTextView = findViewById(R.id.tv_room_number);
                roomNumberTextView.setText("房间号：6301");

                TextView dormManagerTextView = findViewById(R.id.tv_dorm_manager);
                dormManagerTextView.setText("宿管：刘金伟");

                TextView phoneNumberTextView = findViewById(R.id.tv_phone_number);
                phoneNumberTextView.setText("电话：15637309888");
            }
        }
    }

    // 从文件路径获取Bitmap并旋转180度
    private Bitmap getLoacalBitmap(String url) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(url);
            Bitmap originalBitmap = BitmapFactory.decodeStream(fis);
            
            if (originalBitmap != null) {
                // 创建矩阵对象
                Matrix matrix = new Matrix();
                // 旋转180度
                matrix.postRotate(180);
                
                // 创建新的旋转后的位图
                return Bitmap.createBitmap(
                    originalBitmap, 
                    0, 
                    0, 
                    originalBitmap.getWidth(), 
                    originalBitmap.getHeight(), 
                    matrix, 
                    true
                );
            }
            return null;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}